<!DOCTYPE HTML>
<html>
<body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>

<div id="container">
  <div>
    <label id="label1" for="control1">Label 1</label>
    <input id="control1" type="text">
  </div>

  <div>
    <label id="label2">
      Label 2
      <input id="control2" type="text">
    </label>
  </div>

  <div>
    <label id="label3">Label 3</label>
    <input id="control3" type="text">
  </div>

  <div>
    <input id="control4" type="text">
  </div>

  <div>
    <label id="label5">
      Label 5
    </label>
    <input id="control5" type="text">
  </div>

  <div>
    <label id="label6b" for="control6">Label 6b</label>
    <label id="label6c" for="control6">Label 6c</label>
    <input id="control6" type="text">
  </div>
</div>

<script>
async_test((t) => {
    function hasTitleUIElement(axElement) {
        var label1 = accessibilityController.accessibleElementById("label1");
        var titleUIElement = axElement.nameElementAtIndex(0);
        if (titleUIElement == null)
            return false;
        return titleUIElement.role == label1.role;
    }

    function createLabelWithIdAndForAttribute(id, forAttributeValue) {
        var labelElement = document.createElement("label");
        labelElement.id = id;
        labelElement.setAttribute("for", forAttributeValue);
        labelElement.innerText = "Label for " + forAttributeValue;
        return labelElement;
    }

    function reparentNodeIntoContainer(node, container) {
        node.parentElement.removeChild(node);
        container.appendChild(node);
    }

    function axElement(id) {
        return accessibilityController.accessibleElementById(id);
    }

    assert_equals(axElement('control1').nameElementAtIndex(0).isEqual(axElement('label1')), true);

    assert_equals(axElement('control2').nameElementAtIndex(0).isEqual(axElement('label2')), true);

    // Test changing the "for" attribute dynamically.
    assert_equals(hasTitleUIElement(axElement('control3')), false);
    document.getElementById('label3').setAttribute('for', 'control3');
    assert_equals(axElement('control3').nameElementAtIndex(0), axElement('label3'));

    // Test unattached label element that's subsequently attached.
    var label4Element = document.createElement("label");
    label4Element.id = "label4";
    label4Element.setAttribute("for", "control4");
    label4Element.innerText = "Label 4";
    var label4Element = createLabelWithIdAndForAttribute('label4', 'control4');
    assert_equals(hasTitleUIElement(axElement('control4')), false);
    document.getElementById('container').appendChild(label4Element);
    assert_equals(hasTitleUIElement(axElement('control4')), true);
    assert_equals(axElement('control4').nameElementAtIndex(0).isEqual(axElement('label4')), true);

    // Test what happens when the label is detached.
    label4Element.parentElement.removeChild(label4Element);
    assert_equals(hasTitleUIElement(axElement('control4')), false);

    // Test label that gets a control reparented into it.
    assert_equals(hasTitleUIElement(axElement('control5')), false);

    reparentNodeIntoContainer(document.getElementById('control5'), document.getElementById('label5'));
    assert_equals(axElement('control5').nameElementAtIndex(0) != null, true);
    assert_equals(axElement('control5').nameElementAtIndex(0).isEqual(axElement('label5')), true);

    // Make sure the first label is returned, even as labels are added and removed.
    assert_equals(axElement('control6').nameElementAtIndex(0).isEqual(axElement('label6b')), true);
    newLabel6Element = createLabelWithIdAndForAttribute('label6a', 'control6');
    document.body.insertBefore(newLabel6Element, document.body.firstChild);
    assert_equals(axElement('control6').nameElementAtIndex(0).isEqual(axElement('label6a')), true);
    document.body.removeChild(newLabel6Element);
    assert_equals(axElement('control6').nameElementAtIndex(0).isEqual(axElement('label6b')), true);
    t.done();
}, "This tests that titleUIElement works correctly even when things change dynamically.");

</script>

</body>
</html>
